﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.leave;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.leave
{
    public class GetLeaveHandler : IRequestHandler<GetLeaveCommon, ApiPaimg<LeaveDTO>>
    {
        private readonly IBaseRepository<leaveModel> leaveRepo;
        private readonly IBaseRepository<DepartmentModel> deparmentRepo;

        public GetLeaveHandler(IBaseRepository<leaveModel> leaveRepo,IBaseRepository<DepartmentModel> deparmentRepo)
        {
            this.leaveRepo = leaveRepo;
            this.deparmentRepo = deparmentRepo;
        }
        public Task<ApiPaimg<LeaveDTO>> Handle(GetLeaveCommon request, CancellationToken cancellationToken)
        {
            ApiPaimg<LeaveDTO> res=new ApiPaimg<LeaveDTO>();

            var leave = leaveRepo.GetAll().Where(x=>x.IsDelId==false);
            var deparment = deparmentRepo.GetAll();

            var isboosid = deparmentRepo.GetAll().Where(x => x.ParentId.Equals(request.UserId)).Select(s => s.DepartmentId).ToList();

            if (isboosid.Any())
            {
                leave = leave.Where(x => isboosid.Contains(x.DepartmentId));
            }
            else
            {
                // leave = leave.Where(x => x.UserId.Equals(request.UserId));
                leave = leave.Where(x=>x.UserId.Equals(request.UserId));
            }



            if (!string.IsNullOrEmpty(request.leaveName))
            {
                leave = leave.Where(x=>x.leaveName.Contains(request.leaveName));
            }

            if(request.leaveTime!=null)
            {
                leave = leave.Where(x=>x.leaveTime>=request.leaveTime);
            }
            if (request.EndleaveTime!=null)
            {
                DateTime time1 = Convert.ToDateTime(request.EndleaveTime);
                leave = leave.Where(x => x.EndleaveTime < time1.AddDays(1));
            }

            if(request.Sates!=null)
            {
                leave = leave.Where(x => x.Sates==request.Sates);
            }




            var linq = from a in leave
                       join b in deparment
                       on a.DepartmentId equals b.DepartmentId
                       select new LeaveDTO()
                       {
                           leaveId=a.leaveId,
                           leaveName=a.leaveName,
                           leavecause=a.leavecause,
                           CreateTime=a.CreateTime,
                           EndleaveTime=a.EndleaveTime,
                           typeId=a.typeId,
                           leaveTime=a.leaveTime,
                           cause=a.cause,
                           DepartmentId=a.DepartmentId,
                           DepartmentName=b.DepartmentName,
                           DepartmentManager=a.DepartmentManager,
                           IsDelId=a.IsDelId,
                           Number=a.Number,
                           typeIds=a.typeIds,
                           Sates=a.Sates,
                       };
    


            res.ToTaCount = linq.Count();
            res.ToTaPage = (int)Math.Ceiling(res.ToTaCount * 1.0 / request.PageSize);
            res.PageData = linq.OrderByDescending(x => x.leaveId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            res.Message = "审批列表显示";
            return Task.FromResult(res);
        }
    }
}
